package org.transmit;

import java.io.IOException;
import java.util.Map;
import java.util.Random;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.transmit.util.ActionPo;

/**
 * Action转发类
* @Title: DispatchAction.java 
* @Package org.transmit 
* @Description: TODO 
* @author porridge
* @url  http://www.prdblog.com/
* @email  mfkwfc@gmail.com   
* @date 2014年4月20日 上午10:13:05 
* @version V1.0
 */
public class DispatchAction  implements Filter{

	@Override
	public void init(FilterConfig fileconfig) throws ServletException {
		System.out.println("easyprd系统启动中......");
		new StartUtil().init();
	}
	@Override
	public void destroy() {
		ActionContext.removeThread();
		System.out.println("easyprd系统关闭中...");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain fiter) throws IOException, ServletException {
		HttpServletRequest reqs = (HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse)response;
		ActionPo actpo=StartUtil.acts.get(reqs.getServletPath().replace(".action",""));
		if(actpo!=null){//当请求action与系统登记的Action地址是一样的时
			HttpObject htp=null;//=ActionContext.getHttpObject();
			htp=new HttpObject();
			htp.setRequest(reqs);
			htp.setResponse(resp);
			htp.setSession(reqs.getSession());
			htp.setApplication(reqs.getSession().getServletContext());
			StartUtil.setEncodingAndLocale(reqs,resp);//设置编码
			ActionContext.setHttpObject(htp);//将对象存放至 ThreadLocal<HttpObject> 里边.以便其它地方调方.   ThreadLocal 主要是线程保护
			StartUtil.transmitAction(actpo);//转发请求
		}else
			fiter.doFilter(request, response);
	}
}
